#include "GameplayLayer.h"
#include "BelkaSprite.h"
GameplayLayer::~GameplayLayer() {
	if (_angels) {
		_angels->release();
		_angels = NULL;
	}
	if (_levels) {
		_levels->release();
		_levels = NULL;
	}
}
GameplayLayer* GameplayLayer::node() {

	// 'layer' is an autorelease object
	GameplayLayer *layer = GameplayLayer::create();
	return layer;
}

bool GameplayLayer::init() {

	if (!CCLayer::init()) {
		return false;
	}
	levelWidht=2560.0f;
	_angels = new CCArray();
	_levels = new CCArray();
	this->addGameObjects();
	renderHero();
	//this->addScrollingBackground();
	this->scheduleUpdate();
	return true;
}

void GameplayLayer::renderHero()
{
	devilSprite = DevilSprite::utworz();
	devilSprite->setLevelWidth(levelWidht);
	this->addChild(devilSprite,2);
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
	//float halfOfTheScreen = screenSize.width/2.0f;
	this->setPositionX(0.0f);
	CCLog("New hero arrived");
}
void GameplayLayer::addGameObjects(){
/*Jakies poziomy do skakania*/
	_levels->addObject(BelkaSprite::utworz(1,0.0f,30.0f,"pod1",false));
	_levels->addObject(BelkaSprite::utworz(1,190.0f,30.0f,"pod2",false));
	_levels->addObject(BelkaSprite::utworz(1,380.0f,30.0f,"pod3",false));
	_levels->addObject(BelkaSprite::utworz(1,600.0f,150.0f,"pod4",true));
	_levels->addObject(BelkaSprite::utworz(1,900.0f,280.0f,"pod5",false));
	_levels->addObject(BelkaSprite::utworz(1,1360,280,"w1",true));
	_levels->addObject(BelkaSprite::utworz(1,1850,200,"w2",false));
	_levels->addObject(BelkaSprite::utworz(1,2080,200,"w3",true));
	_levels->addObject(BelkaSprite::utworz(1,2300,200,"w4",false));
	_levels->addObject(BelkaSprite::utworz(1,2500,200,"w5",false));

/* linia prosto do konca
	_levels->addObject(BelkaSprite::utworz(1,0.0f,30.0f,"pod1",false));
	_levels->addObject(BelkaSprite::utworz(1,190.0f,30.0f,"pod2",false));
	_levels->addObject(BelkaSprite::utworz(1,380.0f,30.0f,"pod3",false));
	_levels->addObject(BelkaSprite::utworz(1,570.0f,30.0f,"pod4",false));
	_levels->addObject(BelkaSprite::utworz(1,760.0f,30.0f,"pod5",true));
	_levels->addObject(BelkaSprite::utworz(1,570.0f,30.0f,"pod4",false));
	_levels->addObject(BelkaSprite::utworz(1,770.0f,30.0f,"pod4",false));
	_levels->addObject(BelkaSprite::utworz(1,1100.0f,30.0f,"pod4",false));
	_levels->addObject(BelkaSprite::utworz(1,1170.0f,30.0f,"pod4",false));
	_levels->addObject(BelkaSprite::utworz(1,1370.0f,30.0f,"pod4",false));
	_levels->addObject(BelkaSprite::utworz(1,1570.0f,30.0f,"pod4",true));
	_levels->addObject(BelkaSprite::utworz(1,1770.0f,30.0f,"pod4",false));
	_levels->addObject(BelkaSprite::utworz(1,1970.0f,30.0f,"pod4",false));
	_levels->addObject(BelkaSprite::utworz(1,2170.0f,30.0f,"pod4",true));
	_levels->addObject(BelkaSprite::utworz(1,2370.0f,30.0f,"pod4",false));
	_levels->addObject(BelkaSprite::utworz(1,2570.0f,30.0f,"pod4",false));
*/

	CCObject* barO;
	CCARRAY_FOREACH(_levels,barO) {
		BelkaSprite* b = (BelkaSprite*) barO;
		this->addChild(b,2);
		if(b->shouldmonster){
			float bmw= b->getContentSize().width;
			float bmh= b->getContentSize().height;
			float bmx=b->getPosition().x;
			bmx=bmx+(bmw/2);
			float bmy=b->getPosition().y;
			bmy=bmy+(bmh/2);
			renderAngel(bmx,bmy, bmw);
		}

	}
}
void GameplayLayer::adjustLayer(){
	float dXPostision = devilSprite->getPositionX();
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
	float halfOfTheScreen = screenSize.width/2.0f;
	if((dXPostision>halfOfTheScreen)&&(dXPostision<levelWidht-halfOfTheScreen)){
		float newXPostion = halfOfTheScreen-dXPostision;
		this->setPositionX(newXPostion);
	}
}
void GameplayLayer::addScrollingBackground(){
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
	CCSprite *scrollingBackground = CCSprite::create("FlatScrollingLayer.png");
	scrollingBackground->setPosition(ccp(levelWidht/2.0f,screenSize.height/2.0f));
	CCSize imgS = scrollingBackground->boundingBox().size;
	scrollingBackground->setScaleX(levelWidht/imgS.width);
	this->addChild(scrollingBackground,0);
}

void GameplayLayer::connectControlsWithJoystick(SneakyJoystick *leftJ,SneakyButton* jumpBtn){
	leftJoystick=leftJ;
	jumpButton=jumpBtn;
}

void GameplayLayer::update(float deltaTime) {
	//sprawdzenie czy zyje
	if(!devilSprite->isAlive()){
		this->removeChild(devilSprite, true);
		scoreLr->takeLive();
		renderHero();
		return;
	}
	if(devilSprite->isWon()){
		scoreLr->won();
		this->removeChild(devilSprite, true);
		renderHero();
		return;
	}
	//sprawdzenie kolizji z polkami
	bool onBar=false;
	devilSprite->setMaxLeftRight(0.0f,levelWidht);
	CCObject* barO;
	float n=0;
	CCARRAY_FOREACH(_levels,barO) {
		BelkaSprite* bar = (BelkaSprite*) (barO);
		if (devilSprite->boundingBox().intersectsRect(bar->boundingBox())) {
			onBar=true;
			n++;
			devilSprite->collisionWithBar(bar);
			if(n>1){
				break;
			}
		}
	}
	//sprawdzenie kolizji z przeciwnikami
	CCObject* arrayAngel;
	bool angKilled=false;
	CCARRAY_FOREACH(_angels,arrayAngel) {
		CCSprite* angel = (CCSprite*) (arrayAngel);
		if (devilSprite->boundingBox().intersectsRect(angel->boundingBox())) {
			float dy = devilSprite->getPositionY() - (devilSprite->getContentSize().height / 2);
			float ay = angel->getPositionY() - (angel->getContentSize().height / 2);
			if (dy < ay) {
				devilSprite->kill();
			} else {
				this->removeChild(angel, true);
				_angels->removeObject(angel,false);
				scoreLr->addPoint();
			}
		}
	}
	devilSprite->moveCharacter(jumpButton->getIsActive(),leftJoystick->getVelocity(),deltaTime,onBar);
	this->adjustLayer();
}

void GameplayLayer::setScore(ScoreLayer *scl) {
	scoreLr = scl;
}
void GameplayLayer::renderAngel(float startPointX, float startPointY,
		float rangeX) {
	CCSprite *angel = CCSprite::create("angel_1.png");
	angel->setTag(1);
	_angels->addObject(angel);
	startPointY=startPointY+(angel->getContentSize().height/2);
	angel->setPosition(ccp(startPointX,startPointY));

	this->addChild(angel);
//Kod do animowania diabla
	CCAnimation *angelAnimation = CCAnimation::create();
	angelAnimation->addSpriteFrameWithFileName("angel_2.png");
	angelAnimation->addSpriteFrameWithFileName("angel_3.png");
	angelAnimation->addSpriteFrameWithFileName("angel_4.png");
	angelAnimation->addSpriteFrameWithFileName("angel_5.png");
	angelAnimation->addSpriteFrameWithFileName("angel_6.png");
	angelAnimation->addSpriteFrameWithFileName("angel_7.png");
	angelAnimation->setRestoreOriginalFrame(true);
	angelAnimation->setDelayPerUnit(0.5f);

	CCAnimate *angelAnimAction = CCAnimate::create(angelAnimation);
	angelAnimAction->setDuration(0.5f);
	CCRepeatForever *angelRpeatAnimation = CCRepeatForever::create(
			angelAnimAction);
	angel->runAction(angelRpeatAnimation);
	float * z = &rangeX;
	moveAngelNormal(angel, (void*) z);

}
//aniol w 1 strone
void GameplayLayer::moveAngelNormal(CCNode* ang, void* data) {
	float *value = (float*) data;
	//float range = 100.0f;
	((CCSprite*) ang)->setFlipX(false);
	CCPoint currentPos = ang->getPosition();
	CCPoint dest = ccp(currentPos.x - *value , currentPos.y);
	CCFiniteTimeAction* actionMove = CCMoveTo::create(2.0f, dest);
	float *range = new float(*value);
	CCFiniteTimeAction* actionMoveDone = CCCallFuncND::create(ang,
			callfuncND_selector(GameplayLayer::moveAngelRevers), (void*) range);
	ang->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
}
//aniol spowrotem idzie
void GameplayLayer::moveAngelRevers(CCNode* ang, void* data) {
	float *value = (float*) data;
	((CCSprite*) ang)->setFlipX(true);
	CCPoint currentPos = ang->getPosition();
	CCPoint dest = ccp(currentPos.x + *value , currentPos.y);
	CCFiniteTimeAction* actionMove = CCMoveTo::create(2.0f, dest);
	CCFiniteTimeAction* actionMoveDone = CCCallFuncND::create(ang,
			callfuncND_selector(GameplayLayer::moveAngelNormal), (void*) data);
	ang->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
}


